<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 定时更改状态 shwoer_record
 */
class Bath extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 循环处理,赞不需要参数
     */
    public function index()
    {   
        $where['states'] = 1; //预约成功状态
        $where['into_states'] = 0;//还未进入状态 

        $list = \app\common\model\BathRecord::where($where)->select();
        if(!empty($list)&&is_array($list)){
            foreach ($list as $key => $value)
            {
               $minute=floor((time()-$value['add_time'])%86400/60);
               //echo "相差分钟数：".$minute."分钟<br/><br/>";
               //设置的失效时间，从 \application\extra\site.php 中获取
                $site_time = config('site.reserve_expire_time');

                $timediff = time()-$value['add_time'];
                $days = intval($timediff/86400);

                //计算相差的时间为$site_time以上，就改为过期。
                if($minute>$site_time||$days!=0)
                {
                    //更新为已过期
                    $res = \app\common\model\BathRecord::find($value['id']);
                    if($res){
                        $res -> states = 2;
                        $res -> save();

                        //释放预约数量
                        $id = 2;
                        $sult = \app\common\model\ShowerSetting::find($id);
                        if($sult){
                            $finy = ($sult['showered_number']+1);
                            //如果添加上1，大于总数，就把总数给变量数
                            if($finy<$sult['shower_number']){
                                $sult -> showered_number = $finy;
                            }else{
                                $sult -> showered_number = $sult['shower_number'];
                            }
                            $sult -> save();
                        }
                    }//end 更新为过期
                }//end if
            }//end foreach
        }
        $this->success(__('状态更新成功')); 
    }//end 处理结束


    //进入浴室后更新状态，释放名额
    public function into()
    {
        $where['into_states'] = 1; //进入成功状态
        $where['leave_states'] = 0;//还未离开状态 
        $list = \app\common\model\BathRecord::where($where)->select();
        if(!empty($list)&&is_array($list)){
            foreach ($list as $key => $value)
            {
               $minute=floor((time()-$value['into_time'])%86400/60);
               //echo "相差分钟数：".$minute."分钟<br/><br/>";
               //设置的失效时间，从 \application\extra\site.php 中获取
               $site_time = config('site.into_expire_time');

                $timediff = time()-$value['add_time'];
                $days = intval($timediff/86400);
                
                //计算相差的时间为$site_time以上，就改为过期,释放预约成功。
                if($minute>$site_time||$days!=0)
                {
                    //更新为已过期
                    $res = \app\common\model\BathRecord::find($value['id']);
                    if($res){
                        $res -> states = 3; //进入后时间过了洗浴时间，自动释放名额
                        $res -> save();

                        //释放预约数量
                        $id = 2;
                        $sult = \app\common\model\ShowerSetting::find($id);
                        if($sult){
                            $finy = ($sult['showered_number']+1);
                            //如果添加上1，大于总数，就把总数给变量数
                            if($finy<$sult['shower_number']){
                                $sult -> showered_number = $finy;
                            }else{
                                $sult -> showered_number = $sult['shower_number'];
                            }
                            $sult -> save();
                        }
                    }//end 更新为过期
                }//end if
            }//end foreach
        }
        $this->success(__('状态更新成功'));
    }//end into

}
